package pip

import "context"

type (
	FilterContext struct {
		Ctx context.Context
		Err error
		//添加更多上下文传递信息
	}

	FilterResult struct {
		FilterResult map[FilterType][]ElementType //具体结果具体类型 自定义添加map 和type
	}

	FilterHandler interface {
		Filter(ctx *FilterContext, candidateList []ElementType) (result []ElementType, filterResult *FilterResult, err error)
	}

	FilterPipeline interface {
		addLast(name string, handler FilterHandler) FilterPipeline
		addFeature(feature ...int) FilterPipeline
		execHandlerFilter(ctx *FilterContext, candidateList []ElementType) (result []ElementType, filterResult *FilterResult, err error)
	}

	ElementFilter func(ctx *FilterContext, ele ElementType) (bool, FilterType)

	FilterHandlerPlugin interface {
		preFilter(ctx *FilterContext, candidateList []ElementType) //具体类型，具体定义
		postFilter(ctx *FilterContext, candidateList []ElementType, result []ElementType, filterResult *FilterResult, err error)
	}
	// FilterPluginContainer 用于过滤器执行前后 执行的一些规则
	FilterPluginContainer interface {
		FilterHandlerPlugin
		//可以添加更多的plugin 来实现扩展
	}

	FilterPluginContainerBuilder interface {
		AddFilterHandlerPlugin(plugin FilterHandlerPlugin) FilterPluginContainerBuilder
		Build() FilterPluginContainer
	}
)

func NewFilterResult() *FilterResult {
	return &FilterResult{
		FilterResult: make(map[FilterType][]ElementType),
	}
}

func NewFilterContext(ctx context.Context) *FilterContext {
	return &FilterContext{Ctx: ctx}
}
func (r *FilterResult) merge(o *FilterResult) {
	if o != nil {
		for ft, eles := range o.FilterResult {
			r.FilterResult[ft] = eles
		}
	}
}
func (r *FilterResult) append(ele ElementType, tp FilterType) {
	r.FilterResult[tp] = append(r.FilterResult[tp], ele)
}
